package com.lyz.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lyz.subject.application.convert.SubjectAnswerDtoConverter;
import com.lyz.subject.application.convert.SubjectInfoDtoConverter;
import com.lyz.subject.application.dto.SubjectInfoDto;
import com.lyz.subject.common.entity.PageResult;
import com.lyz.subject.common.entity.Result;
import com.lyz.subject.domain.entity.SubjectAnswerBo;
import com.lyz.subject.domain.entity.SubjectInfoBo;
import com.lyz.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 *
 * @author: YizeLee
 * @date: 2024/1/8
 **/
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDto.getSubjectName()),
                    "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getCategoryIds()),
                    "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getLabelIds()),
                    "标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            List<SubjectAnswerBo> subjectAnswerBos =
                    SubjectAnswerDtoConverter.INSTANCE.convertListDtoToBo(subjectInfoDto.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBos);
            subjectInfoDomainService.add(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     */
    @PostMapping("getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getLabelId(), "标签id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            subjectInfoBo.setPageNo(subjectInfoDto.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDto.getPageSize());
            PageResult<SubjectInfoBo> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目失败");
        }
    }

    /**
     * 查询题目详情
     */
    @PostMapping("querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getId(), "题目id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            SubjectInfoBo boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDto dto = SubjectInfoDtoConverter.INSTANCE.convertBoToDto(boResult);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

}
